/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;

import blueprint4j.db.FieldAction;
import blueprint4j.utils.BindFieldInterface;
import blueprint4j.utils.Bindable;
import blueprint4j.utils.Log;


public class DataPairFieldAction extends DataPairDefault implements ActionListener{
    private FieldAction field;
    private JButton btn = null;
    
    public DataPairFieldAction(Binder binder,FieldAction p_field)
    throws java.sql.SQLException {
        super(binder,p_field);
        field=p_field;
        setLabel(new JLabel(field.getName()));
        btn = createButton(p_field);
        btn.addActionListener(this);
        setComponent(btn);
        if (!field.isActionAble()) {
            btn.setEnabled(false);
        }
    }
    
    private JButton createButton(FieldAction fa) {
        JButton btn = null;
        if (fa.getImagePath() != null) {
            try {
                btn = new JButton(Gui.loadIconsFromResource(new String[]{fa.getImagePath()})[0]);
            } catch (Exception ioe){
                Log.critical.out(ioe);
            }
        } else {
            btn = new JButton(fa.getDescription());
        }
        return btn;
    }
    
    public boolean isChanged(){
        return false;
    }
    
    public void intoControl(){}
    
    public void intoBindable(){}
    
    public Bindable getBindable(){
        return field.getActionable().getEntity();
    }
    
    public BindFieldInterface getBindField() {
        return (BindFieldInterface) field;
    }
    
    public void setBindField(BindFieldInterface p_field) {
    }
    
    public void resetControl() {}
    
    public void actionPerformed(ActionEvent event) {
    	try {
	        getBinder().intoBindings();
	        field.action();
	        getBinder().reload();
	        getBinder().intoControls();
        } catch (Throwable th) {
            Log.debug.out(th);
            javax.swing.JOptionPane.showMessageDialog(null,th.getMessage());
        }
    }
}

